//
//  TKIMAudioMsgView.m
//  TKIM
//
//  Created by liupm on 15/10/26.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMAudioMsgView.h"
//#import "TKIMAudioMessage.h"
//#import "TKIMAudioHelper.h"
//#import "TKIMFileHelper.h"

@interface TKIMAudioMsgView()
@property(nonatomic,strong) UIImageView *audioImageView;
@property(nonatomic,strong) TKIMAudioMessage *audioMsg;
@property(nonatomic,strong) UILabel *timeLabel;
@end

@implementation TKIMAudioMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.audioImageView];
    [self.backgroundImageView addSubview:self.timeLabel];
}

#pragma mark  属性

-(UIImageView *)audioImageView
{
    if (!_audioImageView)
    {
        _audioImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    }
    return _audioImageView;
}


-(UILabel *)timeLabel
{
    if (!_timeLabel)
    {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.audioImageView.frame.origin.x+self.audioImageView.frame.size.width , self.audioImageView.frame.origin.y, 45, 35)];
        _timeLabel.font = [UIFont systemFontOfSize:13.0f];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.backgroundColor = [UIColor clearColor];
    }
    return _timeLabel;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    
    _audioMsg = (TKIMAudioMessage *)message;
    NSInteger duration = _audioMsg.audioDuration;
    
    //图片大小
    CGSize size =  CGSizeMake(70+duration*3, 35);
    
    if (size.width > TKIMScreenWidth *0.7){
        size.width = TKIMScreenWidth *0.7;
    }
    
    
    //设置消息label的大小
    CGRect msgRect = CGRectIntegral( [self getMsgFrame:message size:size]);
    
    if(message.isSender){
        self.audioImageView.image = TKIMGetImage(@"im_voice_p3");
        self.timeLabel.frame = CGRectMake(msgRect.origin.x, msgRect.origin.y, self.timeLabel.width, self.timeLabel.height);
        self.audioImageView.frame = CGRectMake(self.backgroundImageView.right-40, (self.backgroundImageView.height-20)/2, self.audioImageView.width, self.audioImageView.height);
        self.timeLabel.textColor = [UIColor blackColor];
        self.audioImageView.animationImages = @[TKIMGetImage(@"im_voice_p1"),TKIMGetImage(@"im_voice_p2"),TKIMGetImage(@"im_voice_p3")];
    }
    else{
        self.audioImageView.image = TKIMGetImage(@"im_voice_p6");
        self.audioImageView.frame = CGRectMake(msgRect.origin.x+5,  (self.backgroundImageView.height-20)/2, self.audioImageView.width, self.audioImageView.height);
        self.timeLabel.frame = CGRectMake(self.backgroundImageView.width -45, msgRect.origin.y, self.timeLabel.width, self.timeLabel.height);
        self.timeLabel.textColor = [UIColor blackColor];
        
        self.audioImageView.animationImages = @[TKIMGetImage(@"im_voice_p4"),TKIMGetImage(@"im_voice_p5"),TKIMGetImage(@"im_voice_p6")];
    }
    
    //如果本地没有声音文件则先下载
    if(![TKIMFileHelper fileIsExist:_audioMsg.localUrl fileSize:[_audioMsg.fileSize longLongValue]] && !_audioMsg.isDownloading)
    {
        _audioMsg.isDownloading = YES;
        [[TKIMManager shareInstance] fetchMessageFile:_audioMsg complete:^(NSObject *obj) {
            _audioMsg.isDownloading = NO;
        } process:^(NSInteger totalBytes, NSInteger bytesWritten) {
            
        } fail:^(NSError *error) {
            
        }];
    }
    
    _audioImageView.animationDuration = 1;
    self.timeLabel.text = [NSString stringWithFormat:@"%ld''", (long)duration];
    
    UITapGestureRecognizer *playAudio = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAudio:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:playAudio];
    if (_audioMsg.isPlaying)
    {
        [self.audioImageView startAnimating];
    }
}


/**
 *  @Author lpm, 15-10-26 17:10:01
 *
 *  @brief  播放声音
 *
 *  @param tap
 */
-(void)playAudio:(UITapGestureRecognizer *)tap
{
    if (_audioMsg.isDownloading)
    {
        return;
    }
    if (_audioMsg.isPlaying)
    {
        _audioMsg.isPlaying = NO;
        [self.audioImageView stopAnimating];
        [self routerEventWithName:TKIMAudioEvent userInfo:@{@"state":@"stop",@"msg":_audioMsg,@"msgView":self}];
    }
    else
    {
        _audioMsg.isPlaying = YES;
        [self.audioImageView startAnimating];
        [self routerEventWithName:TKIMAudioEvent userInfo:@{@"state":@"play",@"msg":_audioMsg,@"msgView":self}];
    }
}


/**
 *  @Author lpm, 15-10-27 09:10:27
 *
 *  @brief  停止播放
 */
-(void)stopPlaying
{
    if (_audioMsg.isPlaying)
    {
        _audioMsg.isPlaying = NO;
        [self.audioImageView stopAnimating];
    }
}


/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    //图片大小
    CGSize size = CGSizeMake(70, 35);
    return   size.height + TKIMChatMsgPadding+14 ;
}
@end